import {HttpHelper} from "./HttpHelper";
import {Stock, StockAlteration} from "./domains";
import {Injectable} from "@angular/core";

@Injectable()
export class StockService {
    stockUrl = "stocks";
    constructor(private httpHelper:HttpHelper){}

    getStocks(page?,size?,store?):Promise<Stock[]> {
        let url = this.stockUrl;
        if (page&&size){
            url = this.stockUrl+ `/?page=${page}&size=${size}`;
        }
        if (store) {
            url = this.stockUrl+ `/?store=${store}`
        }
        if (page&&size&&store){
            url = this.stockUrl+`/?page=${page}&?size=${size}&store=${store}`;
        }
        return this.httpHelper.getJson<Stock[]>(url);
    }

    addStock(stocks):Promise<boolean> {
        return this.httpHelper.postToJson<boolean>(this.stockUrl,stocks);
    }
}